package com.hao.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hao.commonutils.JwtUtils;
import com.hao.commonutils.MD5;
import com.hao.educenter.entity.UcenterMember;
import com.hao.educenter.entity.vo.UcenterMemberVo;
import com.hao.educenter.mapper.UcenterMemberMapper;
import com.hao.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hao.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author hao
 * @since 2020-12-20
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String loginUser(UcenterMember member) {

        //获取登陆手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登陆失败，手机号或密码不能为空");
        }

        //查询数据库，判断手机号或密码,用户是否被禁用
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember dataMember = this.getOne(wrapper);
        if (dataMember == null) {
            throw new GuliException(20001, "登陆失败，用户不存在");
        }
        //密码MD5加密，对比
        password = MD5.encrypt(password);
        if (!password.equals(dataMember.getPassword())) {
            throw new GuliException(20001, "登陆失败，密码错误");
        }
        if (dataMember.getIsDisabled()) {
            throw new GuliException(20001, "登陆失败，用户被封禁");
        }
        //登陆成功,使用jwt生成token
        String token = JwtUtils.getJwtToken(dataMember.getId(), dataMember.getNickname());
        return token;
    }

    @Override
    public void registerUser(UcenterMemberVo vo) {

        //获取数据
        String code = vo.getCode();
        String mobile = vo.getMobile();
        String nickname = vo.getNickname();
        String password = vo.getPassword();
        //空值验证
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new GuliException(20001, "登陆失败，手机号、密码、用户名、验证码都不能为空");
        }

        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "验证码错误");
        }
        //判断手机号是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        int count = this.count(wrapper);
        if (count != 0) {
            throw new GuliException(20001, "手机号已经注册");
        }

        //添加数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        //密码加密
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false);
        //默认头像
        member.setAvatar("https://guli-jiaoyu-kiana.oss-cn-shanghai.aliyuncs.com/2020/11/29/982942dfc49348fdbab5daec62d52b25file.png");
        this.save(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper();
        wrapper.eq("openid",openid);
        UcenterMember member = this.getOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {

        return this.baseMapper.countRegister(day);
    }
}
